home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / ct_luminance.pro < prev    next >
Text File  |  1997-07-08  |  3KB  |  79 lines

  1. ; $Id: ct_luminance.pro,v 1.5 1997/01/15 03:11:50 ali Exp $
  2.  
  3. ; Copyright (c) 1992-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;++NODOCUMENT
  6. ; NAME:
  7. ;    CT_LUMINANCE
  8. ;
  9. ; PURPOSE:
  10. ;    Calculate the luminance of colors.
  11. ;
  12. ; CATEGORY:
  13. ;    Color tables
  14. ;
  15. ; CALLING SEQUENCE:
  16. ;    L = CT_LUMINANCE(R, G, B)
  17. ;
  18. ; INPUTS:
  19. ;    R = Red color table.  If omitted, use the color values from
  20. ;        either the COLORS common block, or the current color table.
  21. ;    G = Green color table, optional parameter.
  22. ;    B = Blue color table, optional parameter.
  23. ;
  24. ; KEYWORD PARAMETERS:
  25. ;    BRIGHT=var - Stores the index of the brightest color in the current
  26. ;        colortable into var.
  27. ;    DARK=var - Stores the index of the darkest color in the current
  28. ;        colortable into var.
  29. ;    READ_TABLES = if set and parameters are not specified,
  30. ;        read directly from color tables, using
  31. ;        TVLCT, /GET.  Do not use the COLORS common block.
  32. ;
  33. ; OUTPUTS:
  34. ;   This function returns an array containing the luminance values
  35. ;    of the specified colors.  If the R,G,B parameters are not
  36. ;    specified, or if R is of integer, byte or long type, the
  37. ;    result is a longword array.  Otherwise, the result is a 
  38. ;    floating point array.
  39. ;
  40. ; COMMON BLOCKS:
  41. ;    COLORS:    Contains the current RGB color tables.
  42. ;
  43. ; MODIFICATION HISTORY:
  44. ;    April 1, 1992, AB
  45. ;        When splitting XPALETTE into widget clusters, this code
  46. ;        became necessary in multiple places. This routine
  47. ;        encapsulates it.
  48. ;    May 15, 1994, DMS
  49. ;        Process colors from parameters or current color table.
  50. ;-
  51.  
  52. function CT_LUMINANCE, R, G, B, DARK=dark, BRIGHT=bright, READ_TABLES=read_it
  53.  
  54.   common colors, r_orig, g_orig, b_orig, r_curr, g_curr, b_curr
  55.  
  56.   ; This is the standard NTSC luminance equation
  57.  
  58.   if n_elements(r) gt 0 then begin    ;Use params?
  59.     lum = .3 * r + .59 * g + .11 * b
  60.     s = size(r)        ;Integer?
  61.     if s[s[0]+1] le 3 then lum = round(lum) < 255  ;Integerize?
  62.   endif else if keyword_set(read_it) then begin    ;Use current color table?
  63.     tvlct, r, g, b, /GET
  64.     lum = ROUND(.3 * r + .59 * g + .11 * b) < 255
  65.   endif else BEGIN            ;Use common block?
  66.       ; Make sure the colors common block is initialized
  67.       if (n_elements(r_curr) eq 0) then begin
  68.         r_orig = bytscl(indgen(!d.table_size))
  69.         g_orig = r_orig & b_orig = r_orig
  70.         r_curr = r_orig & g_curr = r_orig & b_curr = r_orig
  71.       endif
  72.     lum= ROUND(.3 * r_curr + .59 * g_curr + .11 * b_curr) < 255
  73.   endelse
  74.  
  75.   junk = max(lum, bright)    ;Return indices of brightest & darkest entries
  76.   junk = min(lum, dark)
  77.   return, lum
  78. end
  79.